<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的招领 - 校园失物招领系统</title>
    <link rel="stylesheet" href="/css/common.css">
</head>
<body>
    <div class="container">
        <nav class="navbar">
            <div class="nav-left"><h1>校园失物招领系统</h1></div>
            <div class="nav-right">
                <span id="username"></span>
                <a href="/notifications" class="nav-link">通知 <span id="unreadCount" class="badge"></span></a>
                <a href="/profile" class="nav-link">个人中心</a>
                <a href="javascript:void(0)" onclick="logout()" class="nav-link">退出</a>
            </div>
        </nav>

        <div class="main-content">
            <div class="sidebar">
                <a href="/home" class="menu-item">首页</a>
                <a href="/lost" class="menu-item">失物信息</a>
                <a href="/found" class="menu-item">招领信息</a>
                <a href="/lost/publish" class="menu-item">发布失物</a>
                <a href="/found/publish" class="menu-item">发布招领</a>
                <a href="/my/lost" class="menu-item">我的失物</a>
                <a href="/my/found" class="menu-item active">我的招领</a>
                <a href="/claims/sent" class="menu-item">发出的认领</a>
                <a href="/claims/received" class="menu-item">收到的认领</a>
                <a href="/favorites" class="menu-item">我的收藏</a>
                <div id="adminMenu"></div>
            </div>

            <div class="content">
                <h2>我的招领</h2>
                <div class="section">
                    <table>
                        <thead>
                            <tr>
                                <th>标题</th>
                                <th>分类</th>
                                <th>拾取地点</th>
                                <th>拾取时间</th>
                                <th>状态</th>
                                <th>浏览</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="itemList"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        loadUser();
        loadUnreadCount();

        $.get('/api/found/my', function(res) {
            if (res.code === 200) {
                if (res.data.length === 0) {
                    $('#itemList').html('<tr><td colspan="7" class="empty">暂无数据</td></tr>');
                } else {
                    const html = res.data.map(item => `
                        <tr>
                            <td><a href="/found/detail?id=${item.id}">${item.title}</a></td>
                            <td>${item.categoryName}</td>
                            <td>${item.foundLocation}</td>
                            <td>${formatDate(item.foundTime)}</td>
                            <td><span class="status-badge status-${item.status}">${item.status === 0 ? '待认领' : '已归还'}</span></td>
                            <td>${item.views}</td>
                            <td>
                                ${item.status === 0 ? `<button class="btn btn-success" onclick="markReturned(${item.id})">标记归还</button>` : ''}
                                <button class="btn btn-danger" onclick="deleteItem(${item.id})">删除</button>
                            </td>
                        </tr>
                    `).join('');
                    $('#itemList').html(html);
                }
            }
        });

        function markReturned(id) {
            if (confirm('确认已归还该物品？')) {
                $.ajax({
                    url: `/api/found/${id}/status?status=1`,
                    type: 'PUT',
                    success: function(res) {
                        if (res.code === 200) {
                            location.reload();
                        }
                    }
                });
            }
        }

        function deleteItem(id) {
            if (confirm('确定要删除吗？')) {
                $.ajax({
                    url: '/api/found/' + id,
                    type: 'DELETE',
                    success: function(res) {
                        if (res.code === 200) {
                            location.reload();
                        }
                    }
                });
            }
        }
    </script>
</body>
</html>

